//
//  BVLoginViewController.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/13.
//  Copyright © 2016年 com.yinding. All rights reserved.
//  登录

#import "BVLoginViewController.h"
#import "BVRegisterViewController.h"
#import "BVNavItem.h"
#import "BVResetPasswordViewController.h"
#import "BVMineTool.h"
#import "NSString+BVValiMobile.h"
#import "UIView+TransitionAnimation.h"

@interface BVLoginViewController ()
@property (nonatomic, strong) BVMineTool *mineTool;
@property (weak, nonatomic) IBOutlet UITextField *phoneNum;
@property (weak, nonatomic) IBOutlet UITextField *pwd;

@end

@implementation BVLoginViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    self.phoneNum.text = self.user.name;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"登录";
    BVNavItem *registerItem = [[BVNavItem alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [registerItem setTitle:@"注册" forState:UIControlStateNormal];
    [registerItem addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:registerItem];
}

#pragma mark -
#pragma mark - 找回密码(重置密码)
- (IBAction)forgetPasswordClick:(id)sender {
    BVResetPasswordViewController *reset = [[BVResetPasswordViewController alloc] init];
    [self.navigationController pushViewController:reset animated:YES];
}

#pragma mark -
#pragma mark - 注册
- (void)registerClick {
    BVRegisterViewController *registered = [[BVRegisterViewController alloc] init];
    [self.navigationController pushViewController:registered animated:YES];
}
- (IBAction)loginBtnClick:(id)sender {
    [self.phoneNum resignFirstResponder];
    [self.pwd resignFirstResponder];
    
    if (![NSString valiMobile:self.phoneNum.text] || self.pwd.text.length == 0) {
        [self showError:@"账号或密码错误"];
        self.pwd.text = @"";
    } else {
        [self.hud show:YES];
        [self.mineTool loginByMobile:self.phoneNum.text password:self.pwd.text success:^(NSString *status) {
            if ([status isEqualToString:@"0"]) {
                [self.hud hide:YES];
                [self showSuccess:@"登录成功"];
                [BVKeyWindow addTransitionAnimationWithDuration:0.8f type:TransitionOglFlip subType:FROM_LEFT];
                [self.navigationController popToRootViewControllerAnimated:NO];
            } else if ([status isEqualToString:@"10011"]) {
                [self.hud hide:YES];
                [self showError:@"账号或密码错误"];
                self.pwd.text = @"";
            } else if ([status isEqualToString:@"10013"]) {
                [self.hud hide:YES];
                [self showError:@"该账号不存在"];
                self.pwd.text = @"";
                self.phoneNum.text = @"";
            }
        } failure:^(NSError *error) {
            [self.hud hide:YES];
            [self showError:@"未知错误"];
        }];
    }
}

- (IBAction)tap:(id)sender {
    [self.view endEditing:YES];
}
- (BVMineTool *)mineTool {
    if (!_mineTool) {
        _mineTool = [[BVMineTool alloc] init];
    }
    return _mineTool;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
